package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysNotification;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysNotificationService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@Api(tags = "通知管理")
@RestController
@RequestMapping("sys/notification")
public class SysNotificationController {

    @Autowired
    private SysNotificationService sysNotificationService;


    @ApiOperation(value = "查询通知管理")
    @GetMapping("{id}")
    public JsonResult<SysNotification> getNotification(@PathVariable Long id) {
        SysNotification notification = sysNotificationService.getById(id);
        return JsonResult.ok(notification);
    }

    @ApiOperation(value = "分页查询通知管理")
    @GetMapping
    public JsonResult<Page<SysNotification>> findByPage(Page<SysNotification> page, String title, Byte status) {
        Page<SysNotification> notificationPage = sysNotificationService.findByPage(page, title, status);
        return JsonResult.ok(notificationPage);
    }


    @ApiOperation(value = "新增通知管理")
    @PostMapping
    public JsonResult addNotification(@RequestBody SysNotification notification) {
        notification.setId(IdWorker.getId());
        notification.setLastUpdateTime(new Date());
        notification.setCreated(new Date());
        notification.setCreateBy(SecurityUtils.getUserId());
        notification.setStatus((byte) 1);
        boolean save = sysNotificationService.save(notification);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改通知管理")
    @PutMapping
    public JsonResult updateNotification(@RequestBody SysNotification notification) {
        notification.setLastUpdateTime(new Date());
        notification.setModifyBy(SecurityUtils.getUserId());
        boolean update = sysNotificationService.updateById(notification);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改通知管理")
    @PutMapping("modifyStatus")
    public JsonResult modifyStatus(@RequestBody Map<String, Object> map) {
        SysNotification notification = new SysNotification();
        notification.setId(Long.valueOf(String.valueOf(map.get("id"))));
        notification.setStatus(Byte.valueOf(String.valueOf(map.get("status"))));
        notification.setLastUpdateTime(new Date());
        notification.setModifyBy(SecurityUtils.getUserId());
        boolean update = sysNotificationService.updateById(notification);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "删除通知管理")
    @DeleteMapping("{id}")
    public JsonResult deleteNotification(@PathVariable Long id) {
        boolean remove = sysNotificationService.removeById(id);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


}
